package xiaohong.algorithm;


//import xiaohong.data.HeroEntity;

import java.math.BigInteger;
import java.util.*;

public class TwoNumsAdd {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
//        l1.next = new ListNode(6);

        ListNode l2 = new ListNode(3);
        int[] array = {};
        // 将数组添加到链表l2的末尾
        ListNode current = l2;
        while (current.next!= null) {
            current = current.next;
        }
        for (int num : array) {
            current.next = new ListNode(num);
            current = current.next;
        }

        ListNode listNode = addTwoNumbers(l1, l2);
        StringBuilder sb = new StringBuilder();
        while (listNode != null) {
            sb.append(listNode.val);
            listNode = listNode.next;
        }
        System.out.println("sb = " + sb);
    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode(0);
        ListNode cursor = root;
        int carry = 0;
        while(l1 != null || l2 != null || carry != 0) {
            int l1Val = l1 != null ? l1.val : 0;
            int l2Val = l2 != null ? l2.val : 0;
            int sumVal = l1Val + l2Val + carry;
            carry = sumVal / 10;

            ListNode sumNode = new ListNode(sumVal % 10);
            cursor.next = sumNode;
            cursor = sumNode;

            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }

        return root.next;

//        StringBuilder sb1 = new StringBuilder();
//        while (l1!= null) {
//            sb1.append(l1.val);
//            l1 = l1.next;
//        }
//        sb1.reverse();
//        BigInteger carry1 = new BigInteger(sb1.toString());
//        System.out.println("carry1 = " + carry1);
//
//        StringBuilder sb2 = new StringBuilder();
//        while (l2!= null) {
//            sb2.append(l2.val);
//            l2 = l2.next;
//        }
//        sb2.reverse();
//        BigInteger carry2 = new BigInteger(sb2.toString());
//        System.out.println("carry2 = " + carry2);
//        carry1 = carry1.add(carry2);
//        System.out.println("carry1 = " + carry1);
//        ListNode head = null;
//        String result = carry1.toString();
//        // 将字符串的字符添加到List中
//        for (int i = 0; i < result.length(); i++) {
//            int digit = result.charAt(i) - '0';
//            ListNode newNode = new ListNode(digit);
//            newNode.next = head;
//            head = newNode;
//        }
//        return head;
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length && j != i; j++) {
                if (target - nums[j] == nums[i]) {
                    result[0] = i;
                    result[1] = j;
                    return result;
                }
            }
        }
        return result;
    }


    public static int[] twoSum2(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = target - nums[i];
            if (map.containsKey(num)) {
                result[0] = map.get(num);
                result[1] = i;
                return result;
            }
            map.put(nums[i], i);
        }
        return result;
    }


}


class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
